package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.Dto.DishDto;
import com.example.demo.common.R;
import com.example.demo.common.pageResult;
import com.example.demo.entiy.Dish;
import com.example.demo.entiy.DishFlavor;
import com.example.demo.service.DishServiceImp;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishServiceImp dishServiceImp;
    @GetMapping("/page")
    public R<Object> getInfo(int page,int pageSize,String name){

        pageResult<DishDto> dis = dishServiceImp.getInfo(page,pageSize,name);
        return R.success(JSON.toJSON(dis));
    }
    @DeleteMapping
    public R<String> deleteDis(Long[] ids){
        if (dishServiceImp.deletedis(Arrays.asList(ids))){
            return R.success("删除成功！");
        }
        return R.error("商品在出售中");
    }
    @PostMapping("/status/{status}")
    public R<String> ChangeStatus(Long[] ids,@PathVariable int status){
        if (dishServiceImp.changeStatus(status,Arrays.asList(ids))){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @PostMapping
    public R<String> putInfo(HttpServletRequest request, @RequestBody DishDto dishDto){
        Long empId = (Long) request.getSession().getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());


        if (dishServiceImp.putInfo(dishDto)!=0){
                return R.success("添加成功");
            }
        return R.error("添加失败");

    }
    @GetMapping("/{id}")
    public R<DishDto> getDishInfo(@PathVariable Long id){
        DishDto dishDto=dishServiceImp.getDishInfo(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> putDishinfo(HttpServletRequest request, @RequestBody DishDto dishDto){
        Long empId = (Long) request.getSession().getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishDto.setUpdateTime(LocalDateTime.now());


        if (dishServiceImp.UpdataInfo(dishDto)!=0){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @GetMapping("/list")
    public R<List<Dish>> getListdish(Long categoryId,String name){
        List<Dish> dis = dishServiceImp.getDishList(categoryId,name);
        return R.success(dis);
    }
}
